package com.sqy.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqy.dao.ProductMapper;
import com.sqy.entity.Product;
import com.sqy.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>implements ProductService {

    @Autowired
    ProductMapper  productMapper;



    @Override
    public List<Tree<String>> getProductList() {

        List<Product> list = productMapper.selectList(null);

        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
     // 自定义属性名 都要默认值的

        treeNodeConfig.setIdKey("id");
      // 最大递归深度
        treeNodeConfig.setDeep(2);

     //转换器
        List<Tree<String>> treeNodes = TreeUtil.build(list, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId()+"");
                    tree.setParentId(treeNode.getParentId()+"");
                    tree.setName(treeNode.getName());
                    tree.putExtra("title", treeNode.getTitle());
                    tree.putExtra("imgUrl", treeNode.getImgUrl());

                });




        return treeNodes;
    }
}
